﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：ApiRepositoryModule.cs
// 功能描述：TongTongMall.MallWeb.ApiRepository
// 
// Create User：jym 2017/02/28
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Extensions;
using TongTongMall.MallWeb.ApiRepository.Configuration;
using TongTongMall.MallWeb.Core;
using TongTongMall.Modules;

namespace TongTongMall.MallWeb.ApiRepository
{
    [DependsOn(typeof(MallWebCoreModule))]
    public class ApiRepositoryModule : BaseModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }

        public override void PostInitialize()
        {
            SetTcpConfig();
        }

        public override void PreInitialize()
        {
            IocManager.Register<IApiRepositoryModuleConfiguration, ApiRepositoryModuleConfiguration>();
        }

        private void SetTcpConfig()
        {
            var config = IocManager.Resolve<IApiRepositoryModuleConfiguration>();
            if (!config.OAuthUrl.IsNullOrEmpty())
            {
                //授权连接量大时 需要即时断开连接，否则会有大量http连接处于time_wait状态。
                ServicePoint sp = ServicePointManager.FindServicePoint(new Uri(config.OAuthUrl));
                sp.ConnectionLeaseTimeout = 6 * 1000;
            }
        }
    }
}
